/**
 * 
 */
package com.cater.report.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.report.entity.PersonReport;
import com.cater.report.util.RemindDateUtils;
import com.cater.sys.entity.Dep;
import com.cater.sys.entity.User;
import com.cater.sys.service.CompanyBookServiceImpl;
import com.cater.sys.service.DepService;
import com.cater.sys.service.UserService;
import com.cater.talents.service.ResumeServiceImpl;
import com.cater.talents.service.TalentsRecommendServiceImpl;
import com.cater.talents.service.WorkSearchServiceImpl;

/**
 * @Description: TODO
 * @author: yong
 * @time:2018年7月31日 上午6:54:11
 */
@Controller
@RequestMapping("/report")
public class ResumeReportController {
	@Autowired
	private ResumeServiceImpl resumeService;// 简历管理
	@Autowired
	private UserService userService;// 用户
	@Autowired
	private DepService depService;
	@Autowired
	private CompanyBookServiceImpl companyBookService;//
	@Autowired
	private TalentsRecommendServiceImpl recommendService;//
	@Autowired
	private WorkSearchServiceImpl workSearchService;
	@Autowired
	private DemandCommunicateServiceImpl communicateService;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@RequestMapping("/resumeReport")
	public String resumeReport(Model model) {
		List<User> users = userService.getAll();
		model.addAttribute("users", users);
		return "report/resume/create_report";

	}

	/**
	 * 
	 * @Description:统计录入者次数
	 * @param query
	 * @return List<Map<String,String>>
	 * @time:2018年8月8日 下午11:04:45
	 */
	@ResponseBody
	@RequestMapping("/createAnalyze")
	public List<Map<String, String>> CreateAnalyze(CommonQuery query) {
		List<Map<String, String>> maps = null;
		String type = query.getType();
		try {
			if (StringUtils.isNotEmpty(type)) {
				if (type.equals("year")) {
					Date ysdate = RemindDateUtils.getCurrentYearStartTime();
					Date yedate = RemindDateUtils.getCurrentYearEndTime();
					query.setStartDate(sdf.format(ysdate));
					query.setEndDate(sdf.format(yedate));
				} else if (type.equals("helf")) {
					Date sdate = RemindDateUtils.getHalfYearStartTime();
					Date edate = RemindDateUtils.getHalfYearEndTime();
					query.setStartDate(sdf.format(sdate));
					query.setEndDate(sdf.format(edate));
				} else if (type.equals("quarter")) {
					Date qsdate = RemindDateUtils.getCurrentQuarterStartTime();
					Date qedate = RemindDateUtils.getCurrentQuarterEndTime();
					query.setStartDate(sdf.format(qsdate));
					query.setEndDate(sdf.format(qedate));
				} else if (type.equals("month")) {
					Date msdate = RemindDateUtils.getCurrentMonthStartTime();
					Date medate = RemindDateUtils.getCurrentMonthEndTime();
					query.setStartDate(sdf.format(msdate));
					query.setEndDate(sdf.format(medate));
				} else if (type.equals("week")) {
					Date wsdate = RemindDateUtils.getCurrentWeekDayStartTime();
					Date wedate = RemindDateUtils.getCurrentWeekDayEndTime();
					query.setStartDate(sdf.format(wsdate));
					query.setEndDate(sdf.format(wedate));
				}
			}
			maps = resumeService.getAnalysisCreate(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maps;
	}

	@RequestMapping("/resumeUpdate")
	public String resumeUpdate(Model model) {
		List<User> users = userService.getAll();
		model.addAttribute("users", users);
		return "report/resume/update_report";
	}

	@ResponseBody
	@RequestMapping("/updateAnalyze")
	public List<Map<String, String>> UpdateAnalyze(CommonQuery query) {
		List<Map<String, String>> maps = null;
		String type = query.getType();
		try {
			if (StringUtils.isNotEmpty(type)) {
				if (type.equals("year")) {
					Date ysdate = RemindDateUtils.getCurrentYearStartTime();
					Date yedate = RemindDateUtils.getCurrentYearEndTime();
					query.setStartDate(sdf.format(ysdate));
					query.setEndDate(sdf.format(yedate));
				} else if (type.equals("helf")) {
					Date sdate = RemindDateUtils.getHalfYearStartTime();
					Date edate = RemindDateUtils.getHalfYearEndTime();
					query.setStartDate(sdf.format(sdate));
					query.setEndDate(sdf.format(edate));
				} else if (type.equals("quarter")) {
					Date qsdate = RemindDateUtils.getCurrentQuarterStartTime();
					Date qedate = RemindDateUtils.getCurrentQuarterEndTime();
					query.setStartDate(sdf.format(qsdate));
					query.setEndDate(sdf.format(qedate));
				} else if (type.equals("month")) {
					Date msdate = RemindDateUtils.getCurrentMonthStartTime();
					Date medate = RemindDateUtils.getCurrentMonthEndTime();
					query.setStartDate(sdf.format(msdate));
					query.setEndDate(sdf.format(medate));
				} else if (type.equals("week")) {
					Date wsdate = RemindDateUtils.getCurrentWeekDayStartTime();
					Date wedate = RemindDateUtils.getCurrentWeekDayEndTime();
					query.setStartDate(sdf.format(wsdate));
					query.setEndDate(sdf.format(wedate));
				}
			}
			maps = resumeService.getAnalysisUpdate(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maps;
	}

	/**
	 * 
	 * @Description:团队报表
	 * @return String
	 * @time:2019年3月31日 下午6:16:23
	 */
	@RequestMapping("/team")
	public String Team(Model model) {
		List<Dep> deps = depService.getAll();
		model.addAttribute("deps", deps);
		return "report/team";
	}

	@ResponseBody
	@RequestMapping("/getTeam")
	public List<PersonReport> getTeam(CommonQuery query) {
		List<PersonReport> reports = new ArrayList<PersonReport>();
		PersonReport report = null;
		try {
			List<User> users = userService.getAll();
			if (users.size() > 0) {
				for (User user : users) {
					report = new PersonReport();
					report.setName(user.getTruename());
					report.setUserId(user.getId());
					reports.add(report);
				}
			}
			List<Map<String, String>> listcount = companyBookService.getCount(query);
			if (listcount.size() > 0) {
				for (Map<String, String> map : listcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setListCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> createcount = resumeService.getAnalysisCreate(query);
			if (createcount.size() > 0) {
				for (Map<String, String> map : createcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setCreateCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> recommendcount = recommendService.getCount(query);
			if (recommendcount.size() > 0) {
				for (Map<String, String> map : recommendcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setRecommendCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> workrecordcount = workSearchService.getCount(query);
			if (workrecordcount.size() > 0) {
				for (Map<String, String> map : workrecordcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setContactCount(Integer.valueOf(total));
						}
					}
				}
			}
			// String[] matter = {"初试","复试","终试","offer","已过保障期","未过保障期"};
			Integer[] matterNum = { 1, 2, 3, 5, 6, 7 };
			for (int i = 0; i < matterNum.length; i++) {
				query.setClassid(matterNum[i].toString());
				List<Map<String, String>> count = communicateService.getCount(query);
				if (workrecordcount.size() > 0) {
					for (Map<String, String> map : count) {
						Integer userId = Integer.valueOf(map.get("createUserId"));
						String total = String.valueOf(map.get("total"));
						String classid = String.valueOf(map.get("classid"));
						for (PersonReport personReport : reports) {
							if (userId.equals(personReport.getUserId())) {
								if (classid.equals("1")) {
									personReport.setFirstCount(Integer.valueOf(total));
								}
								if (classid.equals("2")) {
									personReport.setSecondCount(Integer.valueOf(total));
								}
								if (classid.equals("3")) {
									personReport.setThirdCount(Integer.valueOf(total));
								}
								if (classid.equals("5")) {
									personReport.setOfferCount(Integer.valueOf(total));
								}
								if (classid.equals("6")) {
									personReport.setNoPassCount(Integer.valueOf(total));
								}
								if (classid.equals("7")) {
									personReport.setPassCount(Integer.valueOf(total));
								}

							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return reports;

	}

	/**
	 * 
	 * @Description:个人报表
	 * @return String
	 * @time:2019年3月31日 下午6:16:23
	 */
	@RequestMapping("/person")
	public String Person(Model model) {
		List<User> users = userService.getAll();
		model.addAttribute("users", users);
		return "report/person";
	}

	@ResponseBody
	@RequestMapping("/getPerson")
	public List<PersonReport> getPerson(CommonQuery query) {
		List<PersonReport> reports = new ArrayList<PersonReport>();
		PersonReport report = null;
		try {
			List<User> users = userService.getAll();
			if (users.size() > 0) {
				for (User user : users) {
					report = new PersonReport();
					report.setName(user.getTruename());
					report.setUserId(user.getId());
					reports.add(report);
				}
			}
			List<Map<String, String>> listcount = companyBookService.getCount(query);
			if (listcount.size() > 0) {
				for (Map<String, String> map : listcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setListCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> createcount = resumeService.getAnalysisCreate(query);
			if (createcount.size() > 0) {
				for (Map<String, String> map : createcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setCreateCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> recommendcount = recommendService.getCount(query);
			if (recommendcount.size() > 0) {
				for (Map<String, String> map : recommendcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setRecommendCount(Integer.valueOf(total));
						}
					}
				}
			}
			List<Map<String, String>> workrecordcount = workSearchService.getCount(query);
			if (workrecordcount.size() > 0) {
				for (Map<String, String> map : workrecordcount) {
					Integer userId = Integer.valueOf(map.get("createUserId"));
					String total = String.valueOf(map.get("total"));
					for (PersonReport personReport : reports) {
						if (userId.equals(personReport.getUserId())) {
							personReport.setContactCount(Integer.valueOf(total));
						}
					}
				}
			}
			// String[] matter = {"初试","复试","终试","offer","已过保障期","未过保障期"};
			Integer[] matterNum = { 1, 2, 3, 5, 6, 7 };
			for (int i = 0; i < matterNum.length; i++) {
				query.setClassid(matterNum[i].toString());
				List<Map<String, String>> count = communicateService.getCount(query);
				if (workrecordcount.size() > 0) {
					for (Map<String, String> map : count) {
						Integer userId = Integer.valueOf(map.get("createUserId"));
						String total = String.valueOf(map.get("total"));
						String classid = String.valueOf(map.get("classid"));
						for (PersonReport personReport : reports) {
							if (userId.equals(personReport.getUserId())) {
								if (classid.equals("1")) {
									personReport.setFirstCount(Integer.valueOf(total));
								}
								if (classid.equals("2")) {
									personReport.setSecondCount(Integer.valueOf(total));
								}
								if (classid.equals("3")) {
									personReport.setThirdCount(Integer.valueOf(total));
								}
								if (classid.equals("5")) {
									personReport.setOfferCount(Integer.valueOf(total));
								}
								if (classid.equals("6")) {
									personReport.setNoPassCount(Integer.valueOf(total));
								}
								if (classid.equals("7")) {
									personReport.setPassCount(Integer.valueOf(total));
								}

							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return reports;

	}
}
